# vim: tabstop=4 shiftwidth=4 expandtab

import fileinput
import re
import os

def _sanitize(string, inAttribute=False):
    string = string.replace('&','&amp;').replace('>','&gt;').replace('<','&lt;')
    if (inAttribute):
        string = string.replace("'","").replace('\\','')
    return string

def convert(lines, inlineImg=False):
    result = []

    preformMode = False
    listMode = False
    imgFileEndings = ['.png','.jpg','.jpeg','.gif','.webp','.svg','.avif','tiff']
    for line in lines:
        matches = re.findall('^```', line)
        if (len(matches) > 0) and (not preformMode):
            result.append("<pre><code>")
            preformMode = True
            continue

        if (len(matches) > 0) and (preformMode):
            result.append("</code></pre>")
            preformMode = False
            continue

        if (preformMode):
            result.append(_sanitize(line.removesuffix("\n")))
            continue

        matches = re.findall('^###(.*)$', line)
        if (len(matches) > 0):
            result.append("<h3>"+_sanitize(matches[0].strip())+"</h3>")
            continue

        matches = re.findall('^##(.*)$', line)
        if (len(matches) > 0):
            result.append("<h2>"+_sanitize(matches[0].strip())+"</h2>")
            continue

        matches = re.findall('^#(.*)$', line)
        if (len(matches) > 0):
            result.append("<h1>"+_sanitize(matches[0].strip())+"</h1>")
            continue
     
        matches = re.findall('^=>\s*(\S+)\s+(.*)$', line)
        if (len(matches) > 0):
            filename, extension = os.path.splitext(matches[0][0].strip())
            linkpath = _sanitize(matches[0][0].strip(), True)
            fileIsImage = inlineImg and extension.lower() in imgFileEndings

            if (fileIsImage) and (matches[0][1]):
                linktext = _sanitize(matches[0][1].strip(), True)
                result.append("<img src='"+linkpath+"' alt='"+linktext+"' />")
            elif (fileIsImage):
                result.append("<img src='"+linkpath+"' />")
            elif (matches[0][1]):
                linktext = _sanitize(matches[0][1].strip())
                result.append("<p><a href='"+linkpath+"'>"+linktext+"</a></p>")
            else:
                result.append("<p><a href='"+linkpath+"'>"+linkpath+"</a></p>")
            continue

        matches = re.findall('^\* (.*)$', line)
        if (len(matches) > 0) and (not listMode):
            listMode = True
            result.append("<ul>")

        if (len(matches) > 0):
            result.append("    <li>"+_sanitize(matches[0].strip())+"</li>")
            continue

        matches = re.findall('^>(.*)$', line)
        if (len(matches) > 0):
            result.append("<blockquote>"+_sanitize(matches[0].strip())+"</blockquote>")
            continue

        if (listMode):
            listMode = False
            result.append("</ul>")

        matches = re.findall('^\s*$', line)
        if (len(matches) > 0):
            continue

        result.append("<p>"+_sanitize(line.strip())+"</p>")

    return result

